﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.Model;
using Way.EAP.DataAccess.Entity;
using Way.Contract.MVC;
using Way.Bus.WMS.Model;

namespace Way.Bus.Wms.Model
{
    /// <summary>
    /// 货位配置管理
    /// </summary>
    [EntityClassAttribut("DepotwbsManage", "货位配置", IsOnAppendProperty = true)]
    public class DepotwbsManage : BasicInfo
    {
        /// <summary>
        /// 客户ID
        /// </summary>
        [EntityForeignKey(typeof(BusinessCustomer), "ID", "CUSTOMERNAME"), EntityControl("客户", false, true, 1), EntityField(20)]
        public int KID { get; set; }
        /// <summary>
        /// 类别ID
        /// </summary>
        [EntityControl("商品类别", false, true, 2)]
        [EntityField(10)]
        [EntityForeignKey(typeof(CommodityClass), "ID", "ClassName")]
        public int LID { get; set; }
        /// <summary>
        /// 商品ID
        /// </summary>
        [EntityControl("商品名称", false, false, 2)]
        [EntityField(10)]
        [EntityForeignKey(typeof(BusinessCommodity),"ID","FNAME")]
        public int MID { get; set; }
        /// <summary>
        /// 仓库ID
        /// </summary>
        [EntityControl("仓库", false, true, 3)]
        [EntityField(10)]
        [EntityForeignKey(typeof(WareHouse), "ID", "WAREHOUSENAME")]
        public int WID { get; set; }
        /// <summary>
        /// 货位ID
        /// </summary>
        [EntityControl("货位", false, true, 4)]
        [EntityField(10)]
        [EntityForeignKey(typeof(WareHouse), "ID", "CODE")]
        public int DID { get; set; }

        /// <summary>
        /// 根据客户ID，仓库ID，货物类别ID获取推荐货位
        /// </summary>
        /// <param name="lid">货物类别ID</param>
        /// <param name="wid">仓库ID</param>
        /// <param name="kid">客户ID</param>
        /// <returns></returns>
        public static string GetDepotHWSQL(int lid, int wid, int kid)
        {
            //完善select字段信息
            string sql = "";

            string sql1 = "select distinct t2.ID,t2.CODE,t2.WHTYPE,t2.WAREHOUSENAME,t2.STATUS"
                + " from DepotwbsManage t1,Acc_WMS_WareHouse t2";

            string w = " where t1.DID=t2.ID and WHTYPE = '2' and STATUS = '0'";//判断类型为“货位”，并且为“可用”状态
            if (kid > 0)
                w += " and KID=" + kid;
            if (wid > 0)
                w += " and WID=" + wid;
            if (lid > 0)
                w += " and LID=" + lid;

            sql = sql1 + w;

            return sql;
        }

        /// <summary>
        /// 根据客户ID，仓库ID，货物类别ID获取推荐货位,该客户所属货位在前，其他可用货位在后
        /// </summary>
        /// <param name="lid">货物类别ID</param>
        /// <param name="wid">仓库ID</param>
        /// <param name="kid">客户ID</param>
        /// <returns></returns>
        public static string GetDepotHWOrderByIdSQL(int lid, int wid, int kid)
        {
            //完善select字段信息---------1125
            string sql = "";

            string sql1 = "select t2.ID, t2.CODE, t2.WAREHOUSENAME, t3.ID as HQID, t3.WAREHOUSENAME as HQName, t2.WHTYPE, t2.STATUS," +
                          " case  when (KID!='') then '1' else '2' end as SID" +
                          " from Acc_WMS_WareHouse t2" +
                          " left join DepotwbsManage t1" +
                          " on t1.DID=t2.ID" +
                          " and KID='" + kid + "'" +
                          " and WID='" + wid + "'" +
                          " and LID='" + lid + "'" +
                          " left join Acc_WMS_WareHouse t3" +
                          " on t3.ID = t2.PARENTID" +
                          " left join Acc_WMS_WareHouse t4" +
                          " on t4.ID = t3.PARENTID";

            string w = " where t2.WHTYPE = '2' and t2.STATUS = '0' and t4.ID='"+ wid +"'";//判断类型为“货位”，并且为“可用”状态

            sql = sql1 + w;

            return sql;
        }

        /// <summary>
        /// 根据客户ID，仓库ID，货物类别ID获取推荐货位,该客户所属货位在前，其他可用货位在后
        /// </summary>
        /// <param name="lid">货物类别ID</param>
        /// <param name="wid">仓库ID</param>
        /// <param name="kid">客户ID</param>
        /// <returns></returns>
        public static string GetDepotHWOrderByIdSQL(int lid, int wid, int kid, string strSQL)
        {
            //完善select字段信息---------1125
            string sql = "";

            string sql1 = "select t2.ID, t2.CODE, t2.WAREHOUSENAME, t3.ID as HQID, t3.WAREHOUSENAME as HQName, t2.WHTYPE, t2.STATUS," +
                          " case  when (KID!='') then '1' else '2' end as SID" +
                          " from Acc_WMS_WareHouse t2" +
                          " left join DepotwbsManage t1" +
                          " on t1.DID=t2.ID" +
                          " and KID='" + kid + "'" +
                          " and WID='" + wid + "'" +
                          " and LID='" + lid + "'" +
                          " left join Acc_WMS_WareHouse t3" +
                          " on t3.ID = t2.PARENTID" +
                          " left join Acc_WMS_WareHouse t4" +
                          " on t4.ID = t3.PARENTID";

            string w = " where t2.WHTYPE = '2' and t2.STATUS = '0' and t4.ID='" + wid + "'";//判断类型为“货位”，并且为“可用”状态

            sql = sql1 + w + strSQL;

            return sql;
        }
    }
}
